﻿Public Class CMAZDALabelState2 : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CMAZDALabelState2.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-M") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask2 = DirectCast(task, CPDATask2)
        Debug.WriteLine("CMAZDALabelState2.DoExecute")
        Dim readText As String = task.ProcessText
        Dim itemCd As String = CCommon.getMazdaItemCd(readText)
        'Dim orderCd As String = CCommon.getMazdaOrderCd(readText)
        Dim orderCd As String = CCommon.trimMazdaLabelOrderCd(CCommon.getMazdaOrderCd(readText))
        Dim uniqueCd As String = readText

        ''読込み済みのラベルではないか確認（ユニークコード）N:アラート
        Dim uniqueCdInfo As CPDATask2.strctUniqueCdInfo = Nothing
        uniqueCdInfo.labelType = CCommon.LABEL_MAZDA
        uniqueCdInfo.itemCd = itemCd
        uniqueCdInfo.uniqueCd = uniqueCd
        uniqueCdInfo.readDateTime = Now()

        Dim retAddUniqueCd As Integer = pdaTask.addUniqueCd(uniqueCdInfo)
        Select Case retAddUniqueCd
            Case -1
                ''アラート
                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        pdaTask.Hardware.alert()
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_015, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        'Debug.WriteLine(CCommon.CNS_MSG_015)

                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
            Case Else
                Dim result As String = ""
                If retAddUniqueCd = 2 Then
                    ''マツダ重複
                    ''アラート
                    Dim result2 As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result2 <> "[YES]" And result <> "[NO]"
                        If result2 = "" Then
                            pdaTask.Hardware.alert()
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_017, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.YESNO_MODE)
                            'Debug.WriteLine(CCommon.CNS_MSG_017)

                        End If
                        result2 = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)

                End If
                If result = "" Or result = "[YES]" Then
                    ''区切りラベル
                    ''（社内ラベルが存在しているかのチェック）
                    If pdaTask.isInChildLabel() Then
                        ''前回のシングル構造体をLabelInfoにセット
                        If pdaTask.addLabelInfo(itemCd, uniqueCd) Then
                            ''新規でシングル構造体を作成
                            pdaTask.clrSingleLabelInfo()
                        End If
                        pdaTask.addSingleLabelInfo(readText, orderCd, itemCd, CCommon.LABEL_MAZDA, 0, False)

                    Else
                        ''エラー
                        pdaTask.Hardware.warning()

                        ''前回読込んだ親ラベルに対して子ラベルが読込まれていない
                        If result = "" Then
                            pdaTask.delLastUniqueCd() ''登録したユニークコードを削除
                        End If


                        Dim result2 As String = ""
                        Dim prevWindowMode As EnumWindowMode
                        Do While result2 <> "[OK]"
                            If result2 = "" Then
                                prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                                pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                                pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                            End If
                            result2 = task.ReadTextWait(-1)
                            'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                        Loop
                        pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                        task.ErrorCd = "ER"
                        task.ProcessText = ""
                        nextState = GetNextState(task, GetType(CErrorState2))
                        Return nextState
                    End If

                End If


        End Select

        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CMAZDALabelState2.DoExit")
        Return True
    End Function


End Class
